package com.yigo.prc.infra.repo;

import com.yigo.framework.http.BException;
import com.yigo.framework.http.RCodeEnum;
import com.yigo.prc.domain.spu.entity.PrcSpu;
import com.yigo.prc.domain.spu.repo.PrcSpuRepo;
import com.yigo.prc.infra.repo.converter.PrcSpuDmo2PoConverter;
import com.yigo.prc.infra.repo.dataobject.PrcSpuPO;
import com.yigo.prc.infra.repo.mapper.PrcSpuMapper;
import lombok.NonNull;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author chenzl
 */
@Repository
public class PrcSpuRepoImpl implements PrcSpuRepo {
    @Resource
    private PrcSpuMapper prcSpuMapper;

    @Override
    public PrcSpu save(@NonNull PrcSpu dmo) {
        PrcSpuPO po = PrcSpuDmo2PoConverter.INSTANCE.toPO(dmo);
        if (null == po.getId()) {
            if (prcSpuMapper.insert(po) == 0) {
                throw new BException(RCodeEnum.PERSIST_OBJECT_ERROR);
            }
            dmo.setId(po.getId());
        } else {
            if (prcSpuMapper.updateById(po) == 0) {
                throw new BException(RCodeEnum.PERSIST_OBJECT_ERROR);
            }
        }
        return dmo;
    }
}
